<?php
namespace Home\Controller;
use Think\Controller;

class StudentController extends Controller {
    //学生信息页
    public function info(){
        if(strlen(session('std_id'))!=0) {
            $std_id = session('std_id');
            $student_info = M('Student')->find($std_id);
            //select * from go_student where Id=$std_id
            $this->assign('student_info', $student_info);
            //get_student函数在Application/Common/function.php内
            $this->assign('std_img', get_student_img($std_id));
            $this->assign('sidebar', 'student');
            $this->display('info');
        }
        else
        {
            $this->redirect('/Home/Index/index',array(), 0, '');
        }
    }
    public function set()
    {
        if(strlen(session('std_id'))!=0) {
            $std_id = session('std_id');
            $info = M('Login')->where(array("std_id"=>$std_id))->limit(1)->select();
            //select * from go_login where std_id=$std_id
            $this->assign('info',$info[0]);
            $this->assign('sidebar', 'student');
            $this->display('set');
        }
        else
        {
            $this->redirect('/Home/Index/index',array(), 0, '');
        }
    }
    public function checkpwd()
    {
        $data['std_id']= I('std_id', '');
        $data['password'] = md5(I('pwd', ''));
        $tf_info=M("Login")->where($data)->getField('Id');
        if(!empty($tf_info)) {
            $data = array(
                'status' => '0',
                'info' => "ok"
            );
        }
        else
        {
            $data = array(
                'status' => '1',
                'info' => '原始密码不正确。'
            );
        }
        $this->ajaxReturn($data);
    }
    public function update_pwd()
    {
        $data['std_id'] = I('std_id', '');
        $data['password'] = md5(I('old_pwd', ''));
        $tf_info = M("Login")->where($data)->getField('Id');
        if (!empty($tf_info)) {
            $data['password'] = md5(I('pwd', ''));
            $login_info = M('Login')->where(array("std_id" => $data['std_id']))->save($data);
            if (strlen($login_info)!=0) {
                $data = array(
                    'status' => '0',
                    'info' => "ok"
                );
            } else {
                $data = array(
                    'status' => '1',
                    'info' => '数据异常请稍候重试。'
                );
            }
        } else {
            $data = array(
                'status' => '1',
                'info' => '原始密码不正确。'
            );
        }
        $this->ajaxReturn($data);
    }
}